﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;
using JYoperation.Class;
using XnaColor = Microsoft.Xna.Framework.Color;
using System.Timers;
using Timer = System.Timers.Timer;
using System.Threading;
using KGFontOpr;
using System.Drawing;
using FisheditIniEdit;
using Roperation;
using MapBase = JYoperation.Class.JYcore.MapBase;
using WinColor = System.Drawing.Color;
using Candy;
namespace RubbishEngine.Mapping
{

    public static class mapping
    {
        public static bool isRain = false;
        public static bool isThunder = false;

        private static int walktickcount = 0;//走路的节拍器
        private static MouseState LastMouseState;
        private static KeyboardState LastKeyboardState;
        private static KeyboardState keyboardstate;
        private static MouseState mousestate;
        private static void noEvents(object A0_0,EventArgs A0_1)
        {
            //Noevents
        }
        private static void mainMenuMed(object A0_0, EventArgs A0_1)
        {
            List<String> tmpstr = new List<string>();
            for (int i = 0; i < 6; i++)
            {
                int Tmpindex = Global.RangerTmp[0].Rdata0[0].Rbyte[12 + i].dataInt;
                if (Tmpindex != -1)
                {
                    string tmpstrName = Global.RangerTmp[1].Rdata0[Tmpindex].Rbyte[4].data.ToString();
                    int tmpstrNameLong = tmpstrName.Length;
                    //自动填充
                    for (int j = 0; j < 5 - tmpstrNameLong; j++)
                        tmpstrName += "　";
                    tmpstrName += Global.RangerTmp[1].Rdata0[Tmpindex].Rbyte[38].data.ToString();
                    tmpstr.Add(tmpstrName);
                }
            }
            Global.uiSurface.addMenu("mainMenuMed", "需要谁治疗？", tmpstr.ToArray(),
                new EventHandler[] { mainMenuMed_Med }, 75, 20);         //Noevents
        }
        //伸展菜单
        private static void mainMenuMed_Med(object A0_0, EventArgs A0_1)
        {
            Global.uiSurface.SetVisible("mainMenuMed");
            List<String> tmpstr = new List<string>();
            for (int i = 0; i < 6; i++)
            {
                int Tmpindex = Global.RangerTmp[0].Rdata0[0].Rbyte[12 + i].dataInt;
                if (Tmpindex != -1)
                {
                    string tmpstrName = Global.RangerTmp[1].Rdata0[Tmpindex].Rbyte[4].data.ToString();
                    int tmpstrNameLong = tmpstrName.Length;
                    //自动填充
                    for (int j = 0; j < 5 - tmpstrNameLong; j++)
                        tmpstrName += "　";
                    tmpstrName += string.Format("{0,-4}/{1,4}", Global.RangerTmp[1].Rdata0[Tmpindex].Rbyte[9].dataInt,
                        Global.RangerTmp[1].Rdata0[Tmpindex].Rbyte[10].dataInt);

                    tmpstr.Add(tmpstrName);
                }
            }
            Global.uiSurface.addMenu("mainMenuMed_Med", "需要治疗谁？", tmpstr.ToArray(),
                new EventHandler[] { mainMenuMed_MedPerson }, 75, 20);         //Noevents
        }
        private static void mainMenuMed_MedPerson(object A0_0, EventArgs A0_1)
        {
            int MedPersonNum = Global.RangerTmp[0].Rdata0[0].Rbyte[
                12 + Global.uiSurface.GetMenuIndex("mainMenuMed")
                ].dataInt;//Global.uiSurface.GetMenuIndex();
            int PersonNum = Global.RangerTmp[0].Rdata0[0].Rbyte[
                12 + Global.uiSurface.GetMenuIndex()
                ].dataInt;//Global.uiSurface.GetMenuIndex();

            int intHurtVal = Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[11].dataInt;
            //////////////////////////////////////////////////////
            //增加生命 = 医疗能力*a+random(5);
            //减低受伤程度 = 医疗能力.
            //当受伤程度>75, a=1/2; 
            //当50<受伤程度<=75, a=2/3;
            //当25<受伤程度<=50, a=3/4;
            //当受伤程度<=25, a=4/5; 
            //
            //当受伤程度>医疗能力+20, 增加生命 = 0, 减低受伤程度 = 0.
            ///////////////////////////////////////////////////////////
            float a = 0;
            if (intHurtVal > 72) a = 0.5f;
            else if (intHurtVal <= 75 && intHurtVal > 50) a = 0.667f;
            else if (intHurtVal <= 50 && intHurtVal > 25) a = 0.75f;
            else if (intHurtVal <= 25) a = 0.8f;
            int intMedVal= Global.RangerTmp[1].Rdata0[MedPersonNum].Rbyte[38].dataInt;
            int intSetMedVal = (int)(intMedVal * a + KGRandom.Ramdom(5));
            int intMedDecHurt  = intMedVal;
            if (intHurtVal > intMedVal + 20) { intSetMedVal = 0; intMedDecHurt = 0; }
            Global.uiSurface.RemoveByTag("mainMenuMed_Med");
            Global.uiSurface.RemoveByTag("mainMenuMed"); 
            Global.uiSurface.addRect("mainMenuMed_AddMed","mainmenu" ,new Microsoft.Xna.Framework.Rectangle(300, 300, 300, 30));
            string PersonName = Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[4].data.ToString();
            Global.uiSurface.addString("mainMenuMed_AddMed", string.Format("{0}增加生命{1}", PersonName, intMedDecHurt), 305, 305);

            //Global.uiSurface.UIlist.Clear();
        }
        private static void mainMenuPoison(object A0_0, EventArgs A0_1)
        {
            List<String> tmpstr = new List<string>();
            for (int i = 0; i < 6; i++)
            {
                int Tmpindex = Global.RangerTmp[0].Rdata0[0].Rbyte[12 + i].dataInt;
                if (Tmpindex != -1)
                {
                    string tmpstrName = Global.RangerTmp[1].Rdata0[Tmpindex].Rbyte[4].data.ToString();
                    int tmpstrNameLong = tmpstrName.Length;
                    //自动填充
                    for (int j = 0; j < 5 - tmpstrNameLong; j++)
                        tmpstrName += "　";
                    tmpstrName += Global.RangerTmp[1].Rdata0[Tmpindex].Rbyte[40].data.ToString().Trim();
                    tmpstr.Add(tmpstrName);
                }
            }
            Global.uiSurface.addMenu("mainMenuPos", "需要谁解毒？", tmpstr.ToArray(),
                new EventHandler[] { mainMenuAttribute_ShowAtt }, 75, 20);         //Noevents
        }
        private static void mainMenuLeave_Exit(object A0_0, EventArgs A0_1)
        {
            Global.jyKdef.Run(1);
            Global.uiSurface.addMenu("mainMenuLeave_Exit", "是否要离开游戏", new string[] { "取消", "确定" },
                new EventHandler[] {mainMenuLeave_Exit_ExitGameCanceled, mainMenuLeave_Exit_ExitGame}, 75 + 55, 20);            //Noevents
        }
        private static void mainMenuLeave_Exit_ExitGame(object A0_0, EventArgs A0_1)
        {
            Global.IsExitGame = true;

        }
        private static void mainMenuDofile(object A0_0, EventArgs A0_1)
        {
            Global.uiSurface.IntIsworkingState = 3;

        }
         private static void mainMenuLeave_Exit_ExitGameCanceled(object A0_0, EventArgs A0_1)
        {
            Global.uiSurface.RemoveByTag(Global.uiSurface.strIsworkingMenu);
        }       
                
        private static void mainMenuLeave(object A0_0, EventArgs A0_1)
        {
            Global.uiSurface.addMenu("MenuQuit", null, new string[] { "存档", "读档", "离开" },
                new EventHandler[] { noEvents, noEvents, mainMenuLeave_Exit}, 75, 20);
        }
        private static void mainMenuAttribute(object A0_0, EventArgs A0_1)
        {
            List<String> tmpstr = new List<string>();
            for(int i = 0;i<6;i++)
            {
                int Tmpindex = Global.RangerTmp[0].Rdata0[0].Rbyte[12 + i].dataInt;
                if(Tmpindex != -1)
                    tmpstr.Add(Global.RangerTmp[1].Rdata0[Tmpindex].Rbyte[4].data.ToString());
            }
            Global.uiSurface.addMenu("MenuAttribute", "查看状态", tmpstr.ToArray(),
                new EventHandler[] {mainMenuAttribute_ShowAtt}, 75, 20);
        }
        private static WinColor cal_att(int val)
        {
            if (val >= 0 && val <= 74)
            {
                return WinColor.White;
            }
            else if (val > 74 && val <= 79)
            {
                return WinColor.LightGreen;
            }
            else if (val > 79 && val <= 89)
            {
                return WinColor.Yellow;
            }
            else if (val > 89 & val <= 94)
            {
                return WinColor.Orange;
            }
            else
            {
                return WinColor.Red;
            }
        }
        private static void mainMenuAttribute_ShowAtt(object A0_0, EventArgs A0_1)
        {
            string strtag = "mainMenuAttribute_ShowAtt";
            int PersonNum = Global.RangerTmp[0].Rdata0[0].Rbyte[12 + Global.uiSurface.GetMenuIndex()].dataInt;//Global.uiSurface.GetMenuIndex();
            Global.uiSurface.addRect(strtag, new Microsoft.Xna.Framework.Rectangle(75 + 105, 20, 600, 560));
            int tmpY = 150;
            //RenderMgr.s
            Global.uiSurface.addTexture(strtag, Global.headTexture[
                Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[1].dataInt
                ], 400, 40, 1.7f);
            string[] tmpstring = new string[] { "姓名", "外号", "等级", "经验值" };//, "体力" };//,"生命","内力","体力"};
            int[] tmpPtr = new int[] { 4, 5, 7, 8};//, 13 };
            for (int i = 0; i < tmpstring.Length; i++)
            {
                Global.uiSurface.addString(strtag, tmpstring[i], 185, tmpY);
                Global.uiSurface.addString(strtag, Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[tmpPtr[i]].data, 185 + 130, tmpY
                    , WinColor.Black, WinColor.White);
                tmpY += 25;
            }
            Global.uiSurface.addString(strtag, "体力", 185, tmpY);
            Global.uiSurface.addString(strtag,
                string.Format("{0,-4}/{1,4}",Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[13].dataInt,100), 185 + 130, tmpY
                , WinColor.Black, WinColor.White);
            tmpY += 25;
           // Global.uiSurface.addString(strtag, "[基本属性]", 185, tmpY, WinColor.Black, WinColor.White);
           // tmpY += 25;
            Global.uiSurface.addString(strtag, "生命", 185, tmpY);
            Global.uiSurface.addString(strtag,
                string.Format("{0,-4}/{1,4}", Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[9].dataInt, Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[10].dataInt)
                , 185 + 130, tmpY, WinColor.Black, WinColor.White);

            tmpY += 25;
            Global.uiSurface.addString(strtag, "内力", 185, tmpY);
            switch (Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[32].dataInt)
            {
                case 2:
                    Global.uiSurface.addString(strtag,
                        string.Format("{0,-4}/{1,4}", Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[33].dataInt, Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[34].dataInt)
                        , 185 + 130, tmpY, WinColor.Black, WinColor.White);
                    break;
                case 1:
                    Global.uiSurface.addString(strtag,
                       string.Format("{0,-4}/{1,4}", Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[33].dataInt, Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[34].dataInt)
                       , 185 + 130, tmpY, WinColor.Brown, WinColor.Yellow);
                    break;
                case 0:
                     Global.uiSurface.addString(strtag,
                        string.Format("{0,-4}/{1,4}", Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[33].dataInt, Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[34].dataInt)
                        , 185 + 130, tmpY,WinColor.FromArgb(200,148,200));//, WinColor.FromArgb(133,62,121));
                    break;
            }
           // tmpY += 25;
           // Global.uiSurface.addString(strtag, "[基本能力]", 185, tmpY, WinColor.Black, WinColor.White);
            tmpY += 25;
            tmpstring = new string[] { "攻击力", "防御力", "轻功" };//,"生命","内力","体力"};
            tmpPtr = new int[] { 35, 37, 36 };
            for (int i = 0; i < tmpstring.Length; i++)
            {
                Global.uiSurface.addString(strtag, tmpstring[i], 185, tmpY);
                string tmpint = Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[tmpPtr[i]].data;
                Global.uiSurface.addString(strtag, tmpint.ToString(), 185 + 130, tmpY
                    , cal_att(int.Parse(tmpint)));
                tmpY += 25;
            }

            //tmpY += 25;
          //  Global.uiSurface.addString(strtag, "[兵器能力]", 185, tmpY,WinColor.Black,WinColor.White);
            //Global.uiSurface.addString(strtag, Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[tmpPtr[i]].data, 185 + 100, tmpY);
            
           // tmpY += 25;
            tmpstring = new string[] { "拳掌能力", "御剑能力", "耍刀技巧" ,"特殊攻击","医疗能力","用毒能力","解毒能力"};//,"生命","内力","体力"};
            tmpPtr = new int[] { 42, 43, 44,45,38,39,40 };
            for (int i = 0; i < tmpstring.Length; i++)
            {
                Global.uiSurface.addString(strtag, tmpstring[i], 185, tmpY);
                string tmpint = Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[tmpPtr[i]].data;
                Global.uiSurface.addString(strtag, tmpint, 185 + 130, tmpY
                    , cal_att(int.Parse(tmpint)));
                tmpY += 25;
            }

            tmpY = 150;
            Global.uiSurface.addString(strtag, "[所会武功]", 485 + 50, tmpY, WinColor.Black, WinColor.White);
            //tmpstring = new string[] { "拳掌能力", "御剑能力", "耍刀技巧", "特殊攻击" };//,"生命","内力","体力"};
            //tmpPtr = new int[] { 42, 43, 44, 45 };
            tmpY += 25;
            for (int i = 0; i < 10; i++)
            {
                //Global.uiSurface.addString(strtag, tmpstring[i], 185, tmpY);
                int tmpKongfuIndex = Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[55 + i].dataInt;
                if (tmpKongfuIndex > 0)
                {
                    string tmpKongfu = string.Format("{0}",
                        Global.RangerTmp[4].Rdata0[tmpKongfuIndex].Rbyte[1].data);
                    Global.uiSurface.addString(strtag, tmpKongfu, 485 + 50, tmpY);

                    string tmpKongIndex = string.Format("{0}",
                        (int)((Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[65 + i].dataInt) / 100 + 1));
                    Global.uiSurface.addString(strtag, tmpKongIndex, 485 + 250, tmpY
                        , WinColor.Black, WinColor.White);
                }
                tmpY += 25; 
            }
            tmpY += 25;
            Global.uiSurface.addString(strtag, "[装备物品]", 485 + 50, tmpY, WinColor.Black, WinColor.White);
            tmpstring = new string[] { "武器","防具"};
            tmpY += 25;
            tmpPtr = new int[] { 15, 16 };//, 44, 45 };
            for (int i = 0; i < tmpstring.Length; i++)
            {
                Global.uiSurface.addString(strtag, tmpstring[i], 485 + 50, tmpY);
                int tmpIndex = Global.RangerTmp[1].Rdata0[PersonNum].Rbyte[tmpPtr[i]].dataInt;
                if (tmpIndex >= 0)
                {
                    Global.uiSurface.addString(strtag, Global.RangerTmp[2].Rdata0[tmpIndex].Rbyte[2].data, 485 + 250, tmpY
                       , WinColor.Black, WinColor.White);
                }
                tmpY += 25;
            }
        }
        public static void simplemaping()
        {


            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;

            mousestate = Mouse.GetState();
            keyboardstate = Keyboard.GetState();
            if (IsPressed(Keys.Enter) || IsPressed(Keys.Space)|| IsPressed(Keys.Escape))
            {
                //Global.uiSurface.DoMenuEvents(Global.uiSurface.strIsworkingMenu);
                Global.uiSurface.RemoveByTag(Global.uiSurface.strIsworkingMenu);
            }

            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;
        }
        public static void smapmaping()
        {

            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;

            mousestate = Mouse.GetState();
            keyboardstate = Keyboard.GetState();
            //bool acceptInput = IsActive;
            //不是在等待状态
            if (Global.SceneM.Mface != 10)
            {
                Global.SceneM.Mface = 9;
            }
            #region scene
            //出场景
            if (((Global.SceneM.X == Global.RangerTmp[3].Rdata0[Global.SceneM.SCENENUM].Rbyte[12].dataInt)
                && (Global.SceneM.Y == Global.RangerTmp[3].Rdata0[Global.SceneM.SCENENUM].Rbyte[15].dataInt)) ||
                ((Global.SceneM.X == Global.RangerTmp[3].Rdata0[Global.SceneM.SCENENUM].Rbyte[13].dataInt)
                && (Global.SceneM.Y == Global.RangerTmp[3].Rdata0[Global.SceneM.SCENENUM].Rbyte[16].dataInt)) ||
                ((Global.SceneM.X == Global.RangerTmp[3].Rdata0[Global.SceneM.SCENENUM].Rbyte[14].dataInt)
                && (Global.SceneM.Y == Global.RangerTmp[3].Rdata0[Global.SceneM.SCENENUM].Rbyte[17].dataInt)))
            {

                Global.SceneM.Brightness = 0.99f;
                Global.SceneM.BrightnessPertime = -0.05f;
                // while (Global.SceneM.Brightness >= 1.0f) ;
                Global.SceneM.SCENENUM = -1;

                //Global.sbuff = Global.jy.DrawMap(Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);

                //Global.BackBuf.SetData(Global.jyScene.DrawScene(Global.SceneM.SCENENUM, Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface));
                if (Global.SceneM.Mface < 4)
                {
                    switch (Global.SceneM.Mface)
                    {
                        case 0:
                            Global.SceneM.MYtmp--;
                            break;
                        case 1:
                            Global.SceneM.MXtmp++;
                            break;
                        case 2:
                            Global.SceneM.MXtmp--;
                            break;
                        case 3:
                            Global.SceneM.MYtmp++;
                            break;
                    }
                }
                else
                {
                    switch (Global.jyScene.mface)
                    {
                        case 0:
                            Global.SceneM.MYtmp--;
                            break;
                        case 1:
                            Global.SceneM.MXtmp++;
                            break;
                        case 2:
                            Global.SceneM.MXtmp--;
                            break;
                        case 3:
                            Global.SceneM.MYtmp++;
                            break;
                    }
                }
                Global.SceneM.X = Global.SceneM.MXtmp;
                Global.SceneM.Y = Global.SceneM.MYtmp;
            }
            #endregion
            if (keyboardstate.IsKeyDown(Keys.Up))
            {
                Global.SceneM.Mface = 0;
                if (Global.SceneM.Y > 0)
                {
                    //if (Global.jyScene.SinData[Global.SceneM.X, Global.SceneM.Y - 1, 1, Global.SceneM.SCENENUM] == 0)
                    if (Global.jyScene.CanWalk(Global.SceneM.X, Global.SceneM.Y - 1, Global.SceneM.SCENENUM))
                    {
                        Global.SceneM.Y--;
                    }
                    Global.sbuff = Global.jyScene.DrawScene(Global.SceneM.SCENENUM, Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);

                }
                return;
            }
            if (keyboardstate.IsKeyDown(Keys.Down))
            {

                //Global.SceneM.Statue = 4;
                Global.SceneM.Mface = 3;
                int bounds = 63;
                if (Global.SceneM.Y < bounds)
                {
                    // if (Global.jyScene.SinData[Global.SceneM.X, Global.SceneM.Y + 1, 1, Global.SceneM.SCENENUM] == 0)
                    if (Global.jyScene.CanWalk(Global.SceneM.X, Global.SceneM.Y + 1, Global.SceneM.SCENENUM))
                    {
                        Global.SceneM.Y++;
                    }
                    Global.sbuff = Global.jyScene.DrawScene(Global.SceneM.SCENENUM, Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
                }
                return;
            }
            if (keyboardstate.IsKeyDown(Keys.Left))
            {
                Global.SceneM.Mface = 2;
                if (Global.SceneM.X > 0)
                {
                    //if (Global.jyScene.SinData[Global.SceneM.X - 1, Global.SceneM.Y,1,Global.SceneM.SCENENUM] == 0)
                    if (Global.jyScene.CanWalk(Global.SceneM.X - 1, Global.SceneM.Y, Global.SceneM.SCENENUM))
                    {
                        Global.SceneM.X--;
                    }
                    Global.sbuff = Global.jyScene.DrawScene(Global.SceneM.SCENENUM, Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
                }
                return;
            }
            if (keyboardstate.IsKeyDown(Keys.Right))
            {
                Global.SceneM.Mface = 1;
                int bounds = 63;
                if (Global.SceneM.X < bounds)
                {
                    //if (Global.jyScene.SinData[Global.SceneM.X + 1, Global.SceneM.Y, 1, Global.SceneM.SCENENUM] == 0)
                    if (Global.jyScene.CanWalk(Global.SceneM.X + 1, Global.SceneM.Y, Global.SceneM.SCENENUM))
                    {
                        Global.SceneM.X++;
                    }
                    Global.sbuff = Global.jyScene.DrawScene(Global.SceneM.SCENENUM, Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
                }
                return;
            }
            if (Global.SceneM.Mface == 9)
            {
                walktickcount++;
                //时序初步定为50
                if (walktickcount > 50)
                {
                    Global.SceneM.Mface = 10;
                    Global.sbuff = Global.jyScene.DrawScene(Global.SceneM.SCENENUM, Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
                    return;
                }
            }
            else
            {
                walktickcount = 0;
                return;
            }
            if (CheckKeyPress(keyboardstate, Keys.G))
            {
                if (isRain == false)
                {
                    Global.SceneM.Brightness = 0.83f;
                }
                else
                {
                    Global.SceneM.Brightness = 1.0f;
                }
                isRain = !isRain;
            }
            if (CheckKeyPress(keyboardstate, Keys.L))
            {
                isThunder = !isThunder;
            }


            if (CheckKeyPress(keyboardstate, Keys.Q))
            {
                RenderMgr.IsHole = !RenderMgr.IsHole;
            }
            if (CheckKeyPress(keyboardstate, Keys.W))
            {
                RenderMgr.IsWave = !RenderMgr.IsWave;
            }
            if (CheckKeyPress(keyboardstate, Keys.D1))
            {
                Global.jy.NowColIndex = 0;
            }
            if (CheckKeyPress(keyboardstate, Keys.D2))
            {
                Global.jy.NowColIndex = 1;
            }
            if (CheckKeyPress(keyboardstate, Keys.D3))
            {
                Global.jy.NowColIndex = 2;
            }
            if (CheckKeyPress(keyboardstate, Keys.D4))
            {
                Global.jy.NowColIndex = 3;
            }
            //////////////////////////////////////////////////////////////////////////////////////////////////
            //滚轮控制镜头
            //////////////////////////////////////////////////////////////////////////////////////////////////
            int wheelvalue = CheckMouseWheel(mousestate);
            if (wheelvalue != 0)
            {
                if (wheelvalue < 0)
                    Global.Zoom = Math.Max(1, Global.Zoom - 0.1f);
                if (wheelvalue > 0)
                    Global.Zoom = Math.Min(10, Global.Zoom + 0.1f);

                MapBase.RealRect = MapBase.WinRect.SetZoom(Global.Zoom);
            }
            Global.sbuff = Global.jyScene.DrawScene(Global.SceneM.SCENENUM, Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
            //    Global.MapMgr.Control(gameTime, mousestate, keyboardstate, acceptInput);
            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;
        }
        public static void eventsmaping()
        {
            if (Global.jyKdef.Run(1))
            {
                return;
            }
        }
        public static void mmapmaping() 
        {
            
            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;
             
            mousestate = Mouse.GetState();
            keyboardstate = Keyboard.GetState();
            //不是在等待状态
            if (Global.SceneM.Mface != 10)
            {
                Global.SceneM.Mface = 9;
            }

            if (Global.jy.MmapCanwalk[Global.SceneM.X, Global.SceneM.Y] != 0)
            {
                Global.SceneM.SCENENUM = Global.jy.MmapCanwalk[Global.SceneM.X, Global.SceneM.Y];
                Global.SceneM.MXtmp = Global.SceneM.X;
                Global.SceneM.MYtmp = Global.SceneM.Y;
                Global.SceneM.X = Global.RangerTmp[3].Rdata0[Global.SceneM.SCENENUM].Rbyte[10].dataInt;
                Global.SceneM.Y = Global.RangerTmp[3].Rdata0[Global.SceneM.SCENENUM].Rbyte[11].dataInt;
                Global.jyScene.mface = Global.SceneM.Tface;
                Global.sbuff = Global.jyScene.DrawScene(Global.SceneM.SCENENUM, Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
                //Global.jyScene.mface = Global.SceneM.Mface;
                return;
            }
            //置顶优先级
            if (CheckKeyPress(keyboardstate, Keys.Escape))
            {
                if (Global.uiSurface.existsTag("mainmenu"))
                {
                    Global.uiSurface.RemoveByTag("mainmenu");
                }
                else
                {
                    Global.uiSurface.addMenu("mainmenu", null, new string[] { "医疗", "解毒", "物品", "状态", "系统" },
                        new EventHandler[] { mainMenuMed, mainMenuPoison, mainMenuDofile, mainMenuAttribute, mainMenuLeave }, 20, 20);
                    //Global.uiSurface.strIsworkingMenu = "mainmenu";
                }
            }

            /////////////////////////////////////////////////////////////////////////
            //优先级处于最后
            ////////////////////////////////////////////////////////////////////////
            if (keyboardstate.IsKeyDown(Keys.Up))
            {
                Global.SceneM.Mface = Global.SceneM.Tface = 0;
                if (Global.SceneM.Y > 0)
                {
                    if (Global.jy.MmapMask[Global.SceneM.X, Global.SceneM.Y - 1] == 0)
                    {
                        Global.SceneM.Y--;
                    }
                    Global.sbuff = Global.jy.DrawMap(Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);

                }
                return;
            }
            if (keyboardstate.IsKeyDown(Keys.Down))
            {
                Global.SceneM.Mface = Global.SceneM.Tface = 3;
                int bounds = 479;
                if (Global.SceneM.Y < bounds)
                {
                    if (Global.jy.MmapMask[Global.SceneM.X, Global.SceneM.Y + 1] == 0)
                    {
                        Global.SceneM.Y++;
                    }
                    Global.sbuff = Global.jy.DrawMap(Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);

                }
                return;
            }
            if (keyboardstate.IsKeyDown(Keys.Left))
            {
                Global.SceneM.Mface = Global.SceneM.Tface = 2;
                if (Global.SceneM.X > 0)
                {
                    if (Global.jy.MmapMask[Global.SceneM.X - 1, Global.SceneM.Y] == 0)
                    {
                        Global.SceneM.X--;
                    }
                    Global.sbuff = Global.jy.DrawMap(Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);

                }
                return;
            }
            if (keyboardstate.IsKeyDown(Keys.Right))
            {
                Global.SceneM.Mface = Global.SceneM.Tface = 1;
                int bounds = 479;
                if (Global.SceneM.X < bounds)
                {
                    if (Global.jy.MmapMask[Global.SceneM.X + 1, Global.SceneM.Y] == 0)
                    {
                        Global.SceneM.X++;
                    }
                    Global.sbuff = Global.jy.DrawMap(Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
                }
                return;
                //}
            }
            if (Global.SceneM.Mface == 9)
            {
                walktickcount++;
                //时序初步定为50
                if (walktickcount > 50)
                {
                    Global.SceneM.Mface = 10;
                    Global.sbuff = Global.jy.DrawMap(Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
                    walktickcount = 0;
                    return;
                    //walktickcount = 0;
                }
            }
            else
            {
                walktickcount = 0;
            }


            //if (CheckKeyPress(keyboardstate, Keys.A))
            //{
            //    Global.SceneM.SCENENUM++;
            //}
            //if (CheckKeyPress(keyboardstate, Keys.S))
            //{
            //    if (Global.SceneM.SCENENUM > 0)
            //        Global.SceneM.SCENENUM--;
            //}



            if (CheckKeyPress(keyboardstate, Keys.G))
            {
                if (isRain == false)
                {
                    Global.SceneM.Brightness = 0.83f;
                }
                else
                {
                    Global.SceneM.Brightness = 1.0f;
                }
                isRain = !isRain;
                isThunder = !isThunder;
            }


            if (CheckKeyPress(keyboardstate, Keys.Q))
            {
                RenderMgr.IsHole = !RenderMgr.IsHole;
            }
            if (CheckKeyPress(keyboardstate, Keys.W))
            {
                RenderMgr.IsWave = !RenderMgr.IsWave;
            }
            if (CheckKeyPress(keyboardstate, Keys.D1))
            {
                Global.jy.NowColIndex = 0;
            }
            if (CheckKeyPress(keyboardstate, Keys.D2))
            {
                Global.jy.NowColIndex = 1;
            }
            if (CheckKeyPress(keyboardstate, Keys.D3))
            {
                Global.jy.NowColIndex = 2;
            }
            if (CheckKeyPress(keyboardstate, Keys.D4))
            {
                Global.jy.NowColIndex = 3;
            }


            //////////////////////////////////////////////////////////////////////////////////////////////////
            //滚轮控制镜头
            //////////////////////////////////////////////////////////////////////////////////////////////////
            int wheelvalue = CheckMouseWheel(mousestate);
            if (wheelvalue != 0)
            {
                if (wheelvalue < 0)
                    Global.Zoom = Math.Max(1, Global.Zoom - 0.1f);
                if (wheelvalue > 0)
                    Global.Zoom = Math.Min(10, Global.Zoom + 0.1f);

                MapBase.RealRect = MapBase.WinRect.SetZoom(Global.Zoom);
            }




            Global.sbuff = Global.jy.DrawMap(Global.SceneM.X, Global.SceneM.Y, Global.SceneM.Mface);
            //    Global.MapMgr.Control(gameTime, mousestate, keyboardstate, acceptInput);
            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;
        }
        public static void titlemaping()
        {
            MouseState mousestate = Mouse.GetState();
            KeyboardState keyboardstate = Keyboard.GetState();
            if (CheckKeyPress(keyboardstate, Keys.Space))
            {
                //RenderMgr.IsHole = !RenderMgr.IsHole;
                Global.SceneM.Statue = 1;
            }
            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;
        }
        public static void menumaping()
        {

            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;
             mousestate = Mouse.GetState();
             keyboardstate = Keyboard.GetState();
            //if (CheckKeyPress(keyboardstate, Keys.Up))
            if(IsPressed(Keys.Up))
            {
                Global.uiSurface.MoveMenu(JYEvents.UIsurface.UiKeys.UiUp);
                return;
            }
            //if (CheckKeyPress(keyboardstate, Keys.Down))
            if(IsPressed(Keys.Down))
            {

                Global.uiSurface.MoveMenu(JYEvents.UIsurface.UiKeys.UiDown);
                return;
            }
            if (IsPressed(Keys.Escape))
            {
                Global.uiSurface.RemoveByTag(Global.uiSurface.strIsworkingMenu);
                return;
            }
            if (IsPressed(Keys.Enter) || IsPressed(Keys.Space))
            {
                Global.uiSurface.DoMenuEvents(Global.uiSurface.strIsworkingMenu);
            }

            LastKeyboardState = keyboardstate;
            LastMouseState = mousestate;
        }
        /// <summary>
        /// Checks whether the specified key or button has been pressed.
        /// </summary>
        private static bool IsPressed(Keys key)
        {
            return (keyboardstate.IsKeyDown(key) &&
                    LastKeyboardState.IsKeyUp(key));
        }
        private static bool CheckKeyPress(KeyboardState keyboardstate, Keys key)
        {
            Keys[] keylst = keyboardstate.GetPressedKeys().Except(LastKeyboardState.GetPressedKeys()).ToArray();

            return keylst.Contains(key);
        }
        private static int CheckMouseWheel(MouseState mousestate)
        {
            return mousestate.ScrollWheelValue - LastMouseState.ScrollWheelValue;
        }
    }
}
